package com.oggero.glossarium.web.actions;

import com.oggero.glossarium.persistence.delegate.GlossarioDelegate;
import com.oggero.glossarium.persistence.models.Vocabolo;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
import java.util.logging.Level;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.RequestAware;

/**
 *
 * @author Alessio
 */
public class InsertVocaboliAction extends ActionSupport implements RequestAware {
    
    private static final String INSERTED = "inserted";
    private static final String FAILED = "error";
    
    static final Logger logger = Logger.getLogger(InsertVocaboliAction.class.getName());

    private GlossarioDelegate glossarioDelegate;
    private Vocabolo vocabolo;
    
    private Map request;
    
    @Override
    public String execute() throws Exception {
        logger.info("InsertVocaboliAction start");
        String forward = INSERTED;
        
        try {
            Vocabolo vocaboloReturn = (Vocabolo) glossarioDelegate.insert(vocabolo);
            request.put("vocaboloRes", vocaboloReturn.getVocaboloIta());
            request.put("result", " inserito correttamente");
        } catch (Exception e) {
            logger.error("insertAction FAILED! ", e);
            forward = FAILED;
        } finally {
            vocabolo.clear();
            logger.info("InsertVocaboliAction stop");
        }
        
        return forward;
    }

    /*
     * Getters e Setters
     */
    public void setGlossarioDelegate(GlossarioDelegate glossarioDelegate) {
        this.glossarioDelegate = glossarioDelegate;
    }

    public Vocabolo getVocabolo() {
        return vocabolo;
    }

    public void setVocabolo(Vocabolo vocabolo) {
        this.vocabolo = vocabolo;
    }

    @Override
    public void setRequest(Map request) {
        this.request = request;
    }
}
